package chuangya.com.anmi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import chuangya.com.anmi.R;
import chuangya.com.anmi.adapter.MainFragmentPageAdapter;
import chuangya.com.anmi.adapter.MainInnerTitleAdapter;
import chuangya.com.anmi.databean.MainInnerTitleBean;

/**
 * Created by Sun.Star on 2017/6/23.
 */

public class MainFragment extends Fragment {
    @InjectView(R.id.main_title_tab)
    RecyclerView mainTitleTab;
    @InjectView(R.id.main_title_search)
    ImageView mainTitleSearch;
    @InjectView(R.id.main_fragment_viewpager)
    ViewPager mainFragmentViewpager;
    private View thisView;
    private MainInnerTitleAdapter adapter;
    private List<MainInnerTitleBean> innerTitleBeenList = new ArrayList<>();
    private List<Fragment> baseFragmentList = new ArrayList<>();
    private MainFragmentPageAdapter pagerAdapter ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        thisView = inflater.inflate(R.layout.main_fragment, null);
        ButterKnife.inject(this, thisView);
        initView();
        initFragment();
        return thisView;
    }

    private void initFragment() {
        baseFragmentList.add(new MainInnerFragment());
        baseFragmentList.add(new MainInnerFragment());
        baseFragmentList.add(new MainInnerFragment());
        baseFragmentList.add(new MainInnerFragment());
        baseFragmentList.add(new MainInnerFragment());
        pagerAdapter = new MainFragmentPageAdapter(getChildFragmentManager(),baseFragmentList);
        mainFragmentViewpager.setAdapter(pagerAdapter);
        mainFragmentViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < innerTitleBeenList.size(); i++) {
                    innerTitleBeenList.get(i).setCheck(false);
                }
                innerTitleBeenList.get(position).setCheck(true);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        MainInnerTitleBean data1 = new MainInnerTitleBean("推荐");
        data1.setCheck(true);
        innerTitleBeenList.add(data1);
        innerTitleBeenList.add(new MainInnerTitleBean("最新"));
        innerTitleBeenList.add(new MainInnerTitleBean("分类"));
        innerTitleBeenList.add(new MainInnerTitleBean("日漫"));
        innerTitleBeenList.add(new MainInnerTitleBean("国漫"));
        adapter = new MainInnerTitleAdapter(getActivity(),innerTitleBeenList);
        GridLayoutManager manager = new GridLayoutManager(getActivity(),5);
        mainTitleTab.setAdapter(adapter);
        mainTitleTab.setLayoutManager(manager);
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mainFragmentViewpager.setCurrentItem(position);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
